local skel = fk.CreateSkill {
  name = "rmt__xuecuchangsheng",
}

Fk:loadTranslationTable{
  ["rmt__xuecuchangsheng"] = "血簇长生",
  [":rmt__xuecuchangsheng"] = "你造成或受到伤害时，可以横置受伤角色并加1点体力上限。你使用摸牌阶段外获得的牌造成伤害后，回复1点体力。",

  ["#rmt__xuecuchangsheng-chain"] = "血簇长生：你可以横置 %src 并加1点体力上限",
  ["@@rmt__xuecuchangsheng-inhand"] = "血簇",

  ["$rmt__xuecuchangsheng1"] = "Their screams are but hymns to immortality.（她们的悲鸣不过是献给永恒的赞歌。）",
  ["$rmt__xuecuchangsheng2"] = "The blood of maidens is the elixir of eternal youth. （少女的鲜血是永葆青春的秘药）",
}

local can_trigger = function (self, event, target, player, data)
  return player:hasSkill(skel.name) and target == player and not data.to.chained
end

local on_cost = function (self, event, target, player, data)
  if player.room:askToSkillInvoke(player, {skill_name = skel.name , prompt = "#rmt__xuecuchangsheng-chain:"..data.to.id }) then
    event:setCostData(self, { tos = {data.to} })
    return true
  end
end

local on_use = function (self, event, target, player, data)
  data.to:setChainState(true)
  if player:isAlive() then
    player.room:changeMaxHp(player, 1)
  end
end

skel:addEffect(fk.DamageCaused, {
  anim_type = "control",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.DamageInflicted, {
  anim_type = "control",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damage, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player:isWounded() and data.card then
      local useParent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if not useParent then return false end
      local use = useParent.data
      return use.card == data.card and use.from == use.from and
      use.extra_data and use.extra_data.rmt__xuecuchangsheng_check
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local subcards = Card:getIdList(data.card)
    if table.find(subcards, function (id)
      return Fk:getCardById(id):getMark("@@rmt__xuecuchangsheng-inhand") ~= 0
    end) then
      data.extra_data = data.extra_data or {}
      data.extra_data.rmt__xuecuchangsheng_check = true
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) and player.phase ~= Player.Draw then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:setCardMark(Fk:getCardById(info.cardId), "@@rmt__xuecuchangsheng-inhand", 1)
          end
        end
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  for _, id in ipairs(player:getCardIds("h")) do
    player.room:setCardMark(Fk:getCardById(id), "@@rmt__xuecuchangsheng-inhand", 0)
  end
end)

return skel
